<template>
  <dialog
    ref="dialogEl"
    class="fav-dialog"
    :style="{ width, height: height ? height : 'fit-content' }"
    :class="!showModel ? 'fixed-log' : ''"
  >
    <div class="fav-dialog-header" v-if="showHeader">
      <span>{{ title }}</span>
      <div class="close-btn" @click="handleClose">
        <img src="@/assets/imgs/icon-close.webp" class="icon-img" alt="" />
      </div>
    </div>
    <slot></slot>
  </dialog>
</template>

<script setup lang="ts">
type Props = {
  width?: string | number;
  height?: string | number;
  title?: string;
  showHeader?: boolean;
  showModel?: boolean;
};

const props = withDefaults(defineProps<Props>(), {
  width: "40%",
  showHeader: true,
  showModel: true,
});
const emit = defineEmits(['close','open'])

const visible: any = defineModel();
const dialogEl = ref(null);

watch(
  () => visible.value,
  (e) => {
    if (e) {
      if (props.showModel) {
        dialogEl.value.showModal();
        emit('open')
      } else {
        dialogEl.value.show();
        emit('open')
      }
    } else {
      dialogEl.value.close();
      emit('close')
    }
  }
);

const handleClose = () => {
  visible.value = false;
  dialogEl.value.close();
};
</script>

<style scoped lang="scss">
.fixed-log {
  position: fixed;
  top: 45%;
}
.fav-dialog {
  background-color: var(--dialog-background);
  border: none;
  padding: 0;
  border-radius: var(--dialog-border-radius);
  width: 800px;
  height: 400px;

  z-index: 100000;
  &:active {
    border: none;
    outline: none;
  }
  &:focus-visible {
    outline: none;
  }
  &-header {
    background: var(--dialog-header-background);
    padding: 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .close-btn {
      &:hover {
        cursor: pointer;
      }
      .icon-img {
        width: 18px;
        height: 18px;
      }
    }
  }
}
</style>
